package com.fyy.java8.optional;

import java.util.Optional;

/**
 * @Author fyy
 * @Description Optional类讲解
 * @Date 2021/3/1 15:35
 */
public class OptionalTest {


    public static void test1(){
        Girl girl = new Girl();
        //正确方式
        Optional<Girl> girl1 = Optional.of(girl);
        //错误方式
        girl = null;
        Optional<Girl> girl2 = Optional.of(girl);
        Optional<Girl> girl3 = Optional.ofNullable(girl);
    }

    public static String getGirlName(Boy boy){
        Optional<Boy> boy1 = Optional.ofNullable(boy);
        //boy2一定不为空
        Boy boy2 = boy1.orElse(new Boy(new Girl("迪丽热巴")));

        Optional<Girl> girl = Optional.ofNullable(boy2.getGirl());
        //girl1一定不为空
        Girl girl1 = girl.orElse(new Girl("古力娜扎"));

        return girl1.getName();
    }

    public static void main(String[] args) {
        Boy boy = null;
        //boy为空
        System.out.println(getGirlName(boy));
        //boy不为空，但是girl为空
        boy = new Boy();
        System.out.println(getGirlName(boy));
        //boy不为空，girl不为空
        boy = new Boy(new Girl("热依扎"));
        System.out.println(getGirlName(boy));
    }

}
